/**
 * Created by CK on 2017/5/17.
 */
$(document).ready(function(){
    var list        = $('#List'),
        startDom    = $('#List_Search_StartTime'),
        endDom      = $('#List_Search_EndTime'),
        optionDom   = $('#List_Search_Option'),
        schoolDom   = $('#List_Search_SchoolName'),
        searchDom   = $('#bt_list_search');

    startDom.datebox('setValue', GetDateStr(-7));
    endDom.datebox('setValue', GetDateStr(-1));

    var startTime   = startDom.getElementValue(),
        endTime     = endDom.getElementValue(),
        queryParams = {
            iStartTime: startTime,
            iEndTime: endTime
        };

    list.initDataGrid(baseUrl + 'Stats/getAppStatsOverview', {queryParams: queryParams});
    searchDom.click(function(){
        getParams();
        list.addDataGridParams(queryParams);
    });
    xhTool.bindDocKeyDown(searchDom);
    $('#bt_list_Export').click(function(){
        getParams();
        location.href = baseUrl + 'Stats/exportAppStatsOverview?' + parseParam(queryParams);
        // window.open(baseUrl + 'Stats/exportAppStatsOverview?' + parseParam(queryParams));
        list.addDataGridParams(queryParams);
    });
    function getParams(){
        startTime = startDom.getElementValue();
        endTime   = endDom.getElementValue();
        if(!startTime || !endTime){
            alert('时间不可为空！');
            return false;
        }
        queryParams.iStartTime = startTime;
        queryParams.iEndTime   = endTime;
        queryParams.sSchoolName= schoolDom.getElementValue();
        queryParams.option     = optionDom.getElementValue();
    }

    var cmenu;
    function createColumnMenu(){
        cmenu = $('<div/>').appendTo('body');
        cmenu.menu({
            onClick: function(item){
                if (item.iconCls == 'fa-check'){
                    list.datagrid('hideColumn', item.name);
                    cmenu.menu('setIcon', {
                        target: item.target
                        //iconCls: 'fa-close'
                    });
                } else {
                    list.datagrid('showColumn', item.name);
                    cmenu.menu('setIcon', {
                        target: item.target,
                        iconCls: 'fa-check'
                    });
                }
            }
        });
        var fields = list.datagrid('getColumnFields');
        for(var i=0; i<fields.length; i++){
            var field = fields[i];
            var col = list.datagrid('getColumnOption', field);
            if(field == 'iSchoolId') continue;
            cmenu.menu('appendItem', {
                text: col.title,
                name: field,
                iconCls: 'fa-check'
            });
        }
    }
    list.datagrid({
        onHeaderContextMenu: function(e, field){
            e.preventDefault();
            if (!cmenu){
                createColumnMenu();
            }
            cmenu.menu('show', {
                left:e.pageX,
                top:e.pageY
            });
        }
    })
});
function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;//获取当前月份的日期
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}
var parseParam = function(param, key){
    var paramStr="";
    if(param instanceof String||param instanceof Number||param instanceof Boolean){
        paramStr+="&"+key+"="+encodeURIComponent(param);
    }else{
        $.each(param,function(i){
            var k=key==null?i:key+(param instanceof Array?"["+i+"]":"."+i);
            paramStr+='&'+parseParam(this, k);
        });
    }
    return paramStr.substr(1);
};